# -*- coding: utf8 -*-

from project.db import DataBase
from project.table_manager import TableManager

class Project( object ):

    _db = None
    _manager = None
    _connection = None

    tables = (
        "option", "directory", "file", "hash", "image",
        "image_vector10", "image_vector_big", "image_vector_numpy", "image_vector_bitstr",
        "image_vector_gray",
        "image2image", "file2file",
    )

    def __init__( self ):
        self._manager = TableManager()
        self._db = DataBase()
        pass

    def getManager(self):
        return self._manager

    def getConnection(self):
        return self._db.getConnection()

    def getTable(self, name):
        return self.getManager().getTable(name)


    def create( self ):
        for name in self.tables:
            self.getTable(name).create()
        return None, True

    def clean( self ):
        for name in self.tables[1:]:
            self.getTable(name).clean()
        return None, True

    def close( self ):
        for name in self.tables:
            self.getTable(name).close()
        self.getConnection().close()
        return None, True


    def save_option( self, name, value ):
        table = "option"
        req = self.getTable(table).save(name, value)
        return req

    def get_option( self, name ):
        table = "option"
        req = self.getTable(table).get(name)
        return req

    def get_options( self ):
        table = "option"
        req = self.getTable(table).options()
        return req

    def vacuum_options( self ):
        table = "option"
        req = self.getTable(table).vacuum()
        return req


    def save_directory( self, parent_id, device, name ):
        """
        @rtype: int
        """
        table = "directory"
        req = self.getTable(table).save(parent_id, device, name)
        return req

    def get_file_full_path( self, item ):
        table = "directory"
        req = self.getTable(table).get_file_full_path(item,)
        return req

    def vacuum_directory( self ):
        table = "directory"
        req = self.getTable(table).vacuum()
        return req


    def insert_file( self, parent_id, device, mime, size, mtime, name ):
        """
        @rtype: int
        """
        table = "file"
        req = self.getTable(table).insert(parent_id, device, mime, size, mtime, name)
        return req

    def remove_file( self, file_id ):
        table = "file"
        req = self.getTable(table).remove(file_id)
        return req

    def get_file( self, file_id ):
        table = "file"
        req = self.getTable(table).get(file_id)
        return req

    def vacuum_file( self ):
        table = "file"
        req = self.getTable(table).vacuum()
        return req


    def insert_hash( self, file_hash ):
        table = "hash"
        req = self.getTable(table).insert(file_hash)
        return req

    def find_hash( self, file_hash ):
        table = "hash"
        req = self.getTable(table).find(file_hash)
        return req

    def vacuum_hash( self ):
        table = "hash"
        req = self.getTable(table).vacuum()
        return req


    def link_hash( self, file_id, hash_id ):
        table = "file"
        req = self.getTable(table).link_hash(file_id, hash_id)
        return req

    def get_file_hash_id( self, file_id ):
        table = "file"
        req = self.getTable(table).get_file_hash_id(file_id)
        return req

    def get_nonuniq_hash_ids( self ):
        table = "file"
        req = self.getTable(table).get_nonuniq_hash_ids()
        return req

    def get_file2hash_by_hash_ids( self, hash_ids ):
        table = "file"
        req = self.getTable(table).get_file2hash_by_hash_ids(hash_ids)
        return req

    def get_files_by_ids( self, file_ids ):
        table = "file"
        req = self.getTable(table).get_by_ids(file_ids)
        return req

    def get_files_count_by_mimes( self, mimes ):
        table = "file"
        req = self.getTable(table).get_count_by_mimes(mimes)
        return req

    def get_files_by_mimes( self, mimes, offset=0, limit=100 ):
        table = "file"
        req = self.getTable(table).get_by_mimes(mimes, offset, limit)
        return req

    def get_hashes_by_ids( self, hash_ids ):
        table = "hash"
        req = self.getTable(table).get_by_ids(hash_ids)
        return req


    def insert_image( self, file_id, mime, size, is_gray,
                      width, height ):
        """
        @rtype: int
        """
        table = "image"
        req = self.getTable(table).insert(
            file_id, mime, size, is_gray, width, height
        )
        return req

    def get_image(self, image_id):
        table = "image"
        req = self.getTable(table).get(image_id)
        return req

    def get_images_count(self):
        table = "image"
        req = self.getTable(table).get_count()
        return req

    def get_images_in_file_count(self, file_id):
        table = "image"
        req = self.getTable(table).get_in_file_count(file_id)
        return req

    def remove_file_images(self, file_id):
        table = "image"
        req = self.getTable(table).remove_file_images(file_id)
        return req

    def get_images(self, count, offset):
        table = "image"
        req = self.getTable(table).get_images(count, offset)
        return req

    def vacuum_image( self ):
        table = "image"
        req = self.getTable(table).vacuum()
        return req

    def clean_image( self ):
        table = "image"
        req = self.getTable(table).clean()
        return req



    def insert_image_v10( self, file_id, image_id,
                          v0, v1, v2, v3, v4, v5, v6, v7, v8, v9 ):
        table = "image_vector10"
        req = self.getTable(table).insert(
            file_id, image_id, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9,)
        return req

    def remove_image_vectors10_by_file(self, file_id):
        table = "image_vector10"
        req = self.getTable(table).remove_file_images(file_id)
        return req

    def get_image_vectors10(self, image_id):
        table = "image_vector10"
        req = self.getTable(table).get(image_id)
        return req

    def get_similar_images10(self, image_vector):
        table = "image_vector10"
        req = self.getTable(table).get_equal(image_vector)
        return req

    def vacuum_image_vector10( self ):
        table = "image_vector10"
        req = self.getTable(table).vacuum()
        return req

    def clean_image_vector10( self ):
        table = "image_vector10"
        req = self.getTable(table).clean()
        return req


    def insert_image_vector_big( self, file_id, image_id, orientation, vector ):
        table = "image_vector_big"
        req = self.getTable(table).insert(file_id, image_id, orientation, vector)
        return req

    def get_image_vectors_big(self, image_id):
        table = "image_vector_big"
        req = self.getTable(table).get(image_id)
        return req

    def remove_image_vectors_big_by_file(self, file_id):
        table = "image_vector_big"
        req = self.getTable(table).remove_by_file(file_id)
        return req

    def vacuum_image_vector_big( self ):
        table = "image_vector_big"
        req = self.getTable(table).vacuum()
        return req

    def clean_image_vector_big( self ):
        table = "image_vector_big"
        req = self.getTable(table).clean()
        return req


    def insert_image_vector_bitstr( self, file_id, image_id, bitmask, vector ):
        table = "image_vector_bitstr"
        req = self.getTable(table).insert(file_id, image_id, bitmask, vector)
        return req

    def get_image_vectors_bitstr(self, image_id):
        table = "image_vector_bitstr"
        req = self.getTable(table).get(image_id)
        return req

    def get_equal_images_bitstr(self, image_id, image_bitmask):
        table = "image_vector_bitstr"
        req = self.getTable(table).get_equal_bitmask(image_id, image_bitmask)
        return req

    def remove_image_vectors_bitstr_by_file(self, file_id):
        table = "image_vector_bitstr"
        req = self.getTable(table).remove_by_file(file_id)
        return req

    def vacuum_image_vector_bitstr( self ):
        table = "image_vector_bitstr"
        req = self.getTable(table).vacuum()
        return req

    def clean_image_vector_bitstr( self ):
        table = "image_vector_bitstr"
        req = self.getTable(table).clean()
        return req


    def insert_image_vector_gray( self, file_id, image_id, vector ):
        table = "image_vector_gray"
        req = self.getTable(table).insert(file_id, image_id, vector)
        return req

    def get_image_vectors_gray(self, image_id):
        table = "image_vector_gray"
        req = self.getTable(table).get(image_id)
        return req

    def remove_image_vectors_gray_by_file(self, file_id):
        table = "image_vector_gray"
        req = self.getTable(table).remove_by_file(file_id)
        return req

    def vacuum_image_vector_gray( self ):
        table = "image_vector_gray"
        req = self.getTable(table).vacuum()
        return req

    def clean_image_vector_gray( self ):
        table = "image_vector_gray"
        req = self.getTable(table).clean()
        return req


    def insert_image_vector_numpy( self, file_id, image_id, vector ):
        table = "image_vector_numpy"
        req = self.getTable(table).insert(file_id, image_id, vector)
        return req

    def get_image_vector_numpy(self, image_id):
        table = "image_vector_numpy"
        req = self.getTable(table).get(image_id)
        return req

    def remove_image_vectors_numpy_by_file(self, file_id):
        table = "image_vector_numpy"
        req = self.getTable(table).remove_by_file(file_id)
        return req

    def vacuum_image_vector_numpy( self ):
        table = "image_vector_numpy"
        req = self.getTable(table).vacuum()
        return req

    def clean_image_vector_numpy( self ):
        table = "image_vector_numpy"
        req = self.getTable(table).clean()
        return req


    def clean_image2image(self):
        table = "image2image"
        req = self.getTable(table).clean()
        return req

    def has_image2image(self, image1_id, image2_id):
        table = "image2image"
        req = self.getTable(table).has(image1_id, image2_id)
        return req

    def insert_image2image( self, image1_id, image2_id, file1_id, file2_id, equal_proc, quality_points):
        table = "image2image"
        req = self.getTable(table).insert(image1_id, image2_id, file1_id, file2_id, equal_proc, quality_points)
        return req

    def remove_file_image2image(self, file_id):
        table = "image2image"
        req = self.getTable(table).remove_file(file_id)
        return req

    def vacuum_image2image( self ):
        table = "image2image"
        req = self.getTable(table).vacuum()
        return req


    def clean_file2file(self):
        table = "file2file"
        req = self.getTable(table).clean()
        return req

    def has_file2file(self, file1_id, file2_id):
        table = "file2file"
        req = self.getTable(table).has(file1_id, file2_id)
        return req

    def insert_file2file( self, file1_id, file2_id, file1_count, file2_count):
        table = "file2file"
        req = self.getTable(table).insert(file1_id, file2_id, file1_count, file2_count)
        return req

    def inc_file2file( self, f2f_id, quality):
        table = "file2file"
        req = self.getTable(table).inc(f2f_id, quality)
        return req

    def get_equal_files(self, diff):
        table = "file2file"
        req = self.getTable(table).get_equal_files(diff)
        return req

    def get_file2file_count(self):
        table = "file2file"
        req = self.getTable(table).get_count()
        return req

    def get_file2file(self, count, offset):
        table = "file2file"
        req = self.getTable(table).get_file2file(count, offset)
        return req

    def remove_file_file2file(self, file_id):
        table = "file2file"
        req = self.getTable(table).remove_file(file_id)
        return req

    def vacuum_file2file( self ):
        table = "file2file"
        req = self.getTable(table).vacuum()
        return req

    pass
